﻿Imports SS.ApplicationBlock
Imports System.IO

Public Class Form1

	Dim intCountAutoRun As Integer = 10
	Dim log As SS.ApplicationBlock.Log

	Private Sub Form1_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
		Try
			log.AddText(Me.txtInfo.Text, SS.ApplicationBlock.Log.MesssageType.Info)
			log.Write()
			Me.SendEmail(Me.txtInfo.Text)
			System.Threading.Thread.Sleep(5000)
		Catch
		End Try
	End Sub

	Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
		System.Threading.Thread.CurrentThread.CurrentCulture = New System.Globalization.CultureInfo("en-GB")
		System.Threading.Thread.CurrentThread.CurrentCulture.DateTimeFormat.ShortDatePattern = "dd/MM/yyyy"
		System.Threading.Thread.CurrentThread.CurrentCulture.DateTimeFormat.DateSeparator = "/"

		lblDesc.Text = "โปรมแกรมจะเริ่มทำงานภายในระยะเวลาที่กำหนด" & Environment.NewLine & "หากต้องการหยุดทำงานกรุณากดปุ่ม ""Stop"""
		log = New SS.ApplicationBlock.Log(SS.ApplicationBlock.Config.GetAppConfig("LogPath"), "IVR")
		TimerAutoRun.Start()
	End Sub

	Private Sub TimerAutoRun_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TimerAutoRun.Tick
		intCountAutoRun -= 1
		Me.btnProcess.Text = "Stop"
		Me.txtInfo.Text &= "โปรมแกรมจะเริ่มใน " & intCountAutoRun.ToString & Environment.NewLine
		If intCountAutoRun <= 0 Then
			TimerAutoRun.Stop()
			Me.btnProcess.Enabled = False
			bgwJob.RunWorkerAsync()
		End If
	End Sub

	Private Sub btnProcess_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnProcess.Click
		If Me.btnProcess.Text.Equals("Stop") Then
			Me.TimerAutoRun.Stop()
			Me.btnProcess.Text = "Start"
		Else
			Me.btnProcess.Enabled = False
			bgwJob.RunWorkerAsync()
		End If
	End Sub

	Private Sub bgwJob_DoWork(ByVal sender As System.Object, ByVal e As System.ComponentModel.DoWorkEventArgs) Handles bgwJob.DoWork
		System.Threading.Thread.CurrentThread.CurrentCulture = New System.Globalization.CultureInfo("en-GB")
		System.Threading.Thread.CurrentThread.CurrentUICulture = New System.Globalization.CultureInfo("en-GB")

		Try
			SetText_ThreadSafe(Me.txtInfo, "Processing..[" & Date.Now & "]" & Environment.NewLine)
			Dim importSBM As New BL.ExternalBoundaryIVR
			importSBM.ImportIVR()
			SetText_ThreadSafe(Me.txtInfo, "Process End.[" & Date.Now & "]")
		Catch ex As Exception
			SetText_ThreadSafe(Me.txtInfo, "Process Fail.")
			SetText_ThreadSafe(Me.txtInfo, Common.LogError.ReadException(ex))
		End Try
	End Sub

	Private Sub bgwJob_RunWorkerCompleted(ByVal sender As Object, ByVal e As System.ComponentModel.RunWorkerCompletedEventArgs) Handles bgwJob.RunWorkerCompleted
		Me.Close()
		Me.Dispose()
	End Sub

	' The delegate
	Delegate Sub SetLabelText_Delegate(ByVal [Label] As TextBox, ByVal [text] As String)

	' The delegates subroutine.
	Private Sub SetText_ThreadSafe(ByVal [Label] As TextBox, ByVal [text] As String)
		' InvokeRequired required compares the thread ID of the calling thread to the thread ID of the creating thread.
		' If these threads are different, it returns true.
		If [Label].InvokeRequired Then
			Dim MyDelegate As New SetLabelText_Delegate(AddressOf SetText_ThreadSafe)
			Me.Invoke(MyDelegate, New Object() {[Label], [text]})
		Else
			[Label].Text &= [text]
			[Label].Select([Label].TextLength, 0)
			[Label].ScrollToCaret()
		End If
	End Sub

	Private Sub btnOpenDir_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnOpenDir.Click
		Dim procstart As New ProcessStartInfo("explorer")
		procstart.Arguments = SS.ApplicationBlock.Config.GetAppConfig("BackupFilePath")
		Process.Start(procstart)
	End Sub

	Private Sub btnViewLog_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnViewLog.Click
		Dim procstart As New ProcessStartInfo("explorer")
		procstart.Arguments = SS.ApplicationBlock.Config.GetAppConfig("LogPath")
		Process.Start(procstart)
	End Sub

	Private Sub SendEmail(ByVal log As String)
		If Config.GetAppConfig("SendEmail").Equals("1") Then
			Dim myReader As New StreamReader(Config.GetAppConfig("MailBodyTemplate"), System.Text.UnicodeEncoding.Default)
			Dim strBody As String = myReader.ReadToEnd.Replace("{ExcuteDate}", Now.ToString("dd/MM/yyyy"))
			strBody = strBody.Replace("{MessageLog}", log)

			Common.Utility.SendMail(Config.GetAppConfig("MailFrom"), Config.GetAppConfig("MailTo"), Config.GetAppConfig("MailCC"), strBody, Config.GetAppConfig("MailSubject"))
		End If
	End Sub

	Public Sub New()

		' This call is required by the designer.
		InitializeComponent()

		' Add any initialization after the InitializeComponent() call.
		' Put the following code before InitializeComponent()

	End Sub
End Class
